/**
 * @Author: ZJoker
 * @Date: 2023-10-14 13:24:12
 * @LastEditTime: 2023-12-26 23:23:26
 * @LastEditors: ZJoker
 * @Description: 按照一定时间间隔轮询调用某函数
 * @FilePath: \jzsy-radio-receiver-client\src\utils\interval-fetch\index.js
 * @生活百般滋味，人生需要笑对
 */
class IntervalFetch {
  constructor(fn, ms = 2000) {
    this.cleared = false
    this.timer = null
    this.fn = fn
    this.ms = ms
    this.intervalFetch()
  }
  clearTimer() {
    this.timer && clearTimeout(this.timer)
    this.timer = null
    this.cleared = true
  }
  pause() {
    this.cleared = true
  }
  restart() {
    this.cleared = false
    this.intervalFetch()
  }
  async intervalFetch() {
    if (this.cleared) {
      return
    }
    await this.fn()
    this.timer = setTimeout(() => {
      this.intervalFetch()
    }, this.ms)
  }
}
export default IntervalFetch
